#!/bin/bash

function _detect_platform()
{
    PLATFORM=`uname -p`
    if [ $PLATFORM = 'unknown' ]; then
       PLATFORM="i386"
    fi
    if [ $PLATFORM = 'i686' ]; then
       PLATFORM="ia64"
    fi
    echo "Detect platform = $PLATFORM"
    
    if [ -f /etc/redhat-release ]
    then
        DISTRO=`rpm -q --queryformat='%{VERSION}' redhat-release  | awk -F '.' '{ gsub(/[a-zA-Z]+$/, "", $1); print "rhel" $1}'`
    elif   [ -f /etc/suse-release ] || [ ! -z "`cat /etc/issue | grep SUSE | grep Linux`" ]
    then
        DISTRO=`rpm -q --queryformat='%{VERSION}' sles-release  | awk '{print "sles" $1}'`
    fi
    echo "Detect linux distro = $DISTRO"
}

# ia64-standalong | x86-standalone | ia64-vms
TARGET=$1
RPM=$2

HPSMX="/opt/hp/hp-smx"
INSTALL="build/$TARGET/install"
_detect_platform

# ld.so.conf configuration
mkdir -p /etc/ld.so.conf.d
echo $HPSMX/lib > /etc/ld.so.conf.d/hp-smx.conf

# Test files
echo "Installing test files ......"
mkdir -p $HPSMX/tests
mkdir -p $HPSMX/sbin
mkdir -p $HPSMX/docs
mkdir -p $HPSMX/dataSheets
cp -R   $INSTALL/tests       $HPSMX
cp -R   $INSTALL/sbin        $HPSMX
cp -R   $INSTALL/docs        $HPSMX
cp -R   $INSTALL/dataSheets  $HPSMX


# For Pegasus
if [ "$RPM" == "pegasus" ]
then
    echo "Installing binaries ......"
    mkdir -p /var/lib/hp-smx
    mkdir -p $HPSMX/lib
    mkdir -p $HPSMX/bin
    cp      $INSTALL/lib/*.so $HPSMX/lib
    cp      $INSTALL/bin/*    $HPSMX/bin
    
    echo "Installing providers ......"
    mkdir -p /usr/lib/Pegasus/providers
    cp      $INSTALL/cmpi/*.so /usr/lib/Pegasus/providers

    echo "Installing mof files and documents ......"
    mkdir -p $HPSMX/mof/root/hpq
    mkdir -p $HPSMX/mof/root/interop
    mkdir -p $HPSMX/mof/CIM2191
    cp -R   $INSTALL/mofs/root/hpq $HPSMX/mof/root
    cp -R   $INSTALL/mofs/root/interop $HPSMX/mof/root
    cp -R   $INSTALL/mofs/*moflist* $HPSMX/mof
    tar -xf $INSTALL/mofs/CIM2191/cimv2191Final-MOFs.tar -C $HPSMX/mof/CIM2191
    cp -R   $INSTALL/mofs/cimv2191-coreinterop.mof $HPSMX/mof
    cp -R   $INSTALL/reg $HPSMX
    
    echo "Post works ......"
    if [ $DISTRO == "rhel5" ]
    then
        cimconfig -s forceProviderProcesses=true -p > /dev/null
        setsebool -P pegasus_disable_trans=1
    fi

    /etc/init.d/tog-pegasus status > /dev/null
    
    if [ $? == 0 ]; then
        /etc/init.d/tog-pegasus restart
    fi
fi

# For sfcb
if [ "$RPM" == "sfcb" ]
then
    echo "Installing binaries ......"
    mkdir -p /var/lib/hp-smx
    mkdir -p $HPSMX/lib
    mkdir -p $HPSMX/bin
    cp $INSTALL/lib/lib*  $HPSMX/lib
    cp $INSTALL/bin/*     $HPSMX/bin
    
    echo "Installing providers ......"
    mkdir -p /usr/lib/cmpi
    cp $INSTALL/cmpi/lib*                /usr/lib/cmpi

    echo "Installing mof files and documents ......"
    mkdir -p /var/lib/sfcb/stage/mofs/root/interop
    mkdir -p /var/lib/sfcb/stage/mofs/root/hpq
    mkdir -p /var/lib/sfcb/stage/regs
    mkdir -p /var/lib/sfcb/stage/bin
    cp $INSTALL/mofs/root/interop/*.mof  /var/lib/sfcb/stage/mofs/root/interop
    cp $INSTALL/mofs/root/hpq/*          /var/lib/sfcb/stage/mofs/root/hpq
    cp $INSTALL/reg/SMX*.reg             /var/lib/sfcb/stage/regs/
    
    # Rename SMXQualifiers.mof as sfcbrepos doesn't seem to work in the
    # presence of additional qualifier definitions.  Making sure the
    # qualifiers mof is first in the directory listing seems to fix things.
    for ns in root/hpq root/interop; do
        mv $INSTALL/mofs/$ns/SMXQualifiers.mof  $INSTALLmofs/$ns/00SMXQualifiers.mof
    done
    
    echo "Post works ......"
    # stop sfcb
    $HPSMX/bin/stop_sfcb.sh
    killall -q sfcbd > /dev/null
    
    # rebuild repository and restart sfcb
    ldconfig  > /dev/null 2>/dev/null
    /usr/bin/sfcbrepos -f
    
    $HPSMX/bin/start_sfcb.sh
    exit 0
fi



